// pages/login/index.js
let util = require('../../utils/util.js')

import {
  wechat,
  member,
  staff
} from "../../api/apiData"
import Toast from '@vant/weapp/toast/toast';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    username: "",
    password: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.login({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setusername(e) {
    var val = e.detail.value
    this.setData({
      username: val
    })
  },
  setpassword(e) {
    var val = e.detail.value
    this.setData({
      password: val
    })
  },
  Login() {
    let {
      username,
      password
    } = this.data
    member.station.memberLogin({
      account: username,
      password: util.AesEncode(password)
    }, res => {
      let {
        code,
        msg,
        data
      } = res
      if (code == 1) {
        wx.showToast({
          title: '登录成功',
        })
        wx.setStorage({
          key: "token2",
          data: data.token
        })
        wx.setStorage({
          key: "type",
          data: 'Member'
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }

    })
  },
  VisiLogin() {
    let _this = this
    wx.showLoading({
      title: "请稍等...",
    })
    let iv, encryptedData
    wx.getUserProfile({
      desc: "个人信息",
      success: function (res) {
        iv = res.iv
        encryptedData = res.encryptedData
        wx.login({
          success(res) {
            wechat.login({
              code: res.code,
              iv,
              encryptedData
            }, res => {
              let {
                code,
                msg,
                data
              } = res
              console.log(res)
              if (code == 0) {
                wx.showToast({
                  title: res.msg,
                  icon: 'error'
                })
              } else {
                wx.showToast({
                  title: res.msg,
                })
                wx.setStorage({
                  key: "token",
                  data: data.token
                })
                wx.setStorage({
                  key: "type",
                  data: 'User'
                })
                wx.navigateBack({
                  delta: 0,
                })
              }

            })
          },
          fail(err) {
            wx.hideLoading();
          }
        })
      },
      fail(err) {
        wx.hideLoading();
      }
    })
  },
  WxLogin() {
    wx.login({
      success(res) {
        let {
          code
        } = res
        member.wxapp.login({
          code
        }, res => {
          let {
            msg,
            data
          } = res
          if (res.code == 1) {
            wx.showToast({
              title: res.msg,
            })
            wx.setStorage({
              key: "token2",
              data: data.token
            })
            wx.setStorage({
              key: "type",
              data: 'Member'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 0,
              })
            }, 1000)
          } else {
            wx.showToast({
              title: res.msg,
              icon:'error'
            })
          }
        })

      }
    })

  }
})